package com.chushouya.controller.manager.admin;

import com.chushouya.common.constant.ServiceType;
import com.chushouya.manager.dto.admin.partner.*;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.chushouya.manager.service.admin.PartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 渠道与合作商
 */
@RestController
@RequestMapping(ServiceType.PLATFORM + "/partner")
@Api(tags = ServiceType.PLATFORM + "渠道管理")
public class PartnerController {

    @Resource
    private PartnerService partnerService;

    @GetMapping("/page")
    @ApiOperation("查询合作商列表")
    public Response<PageResult<PartnerListDTO>> page(PartnerQuery query) {
        return Response.success(partnerService.selectPage(query));
    }

    @GetMapping("/detail")
    @ApiOperation("查询合作商详情")
    public Response<PartnerDetailDTO> detail(Long partnerId) {
        return Response.success(partnerService.detail(partnerId));
    }

    @PostMapping("/add")
    @ApiOperation("新增合作商")
    @Log(businessType = BusinessType.INSERT)
    public Response<Void> save(@RequestBody @Valid PartnerSaveDTO dto) {
        partnerService.save(dto);
        return Response.success();
    }

    @PostMapping("/update")
    @ApiOperation("编辑合作商")
    @Log(businessType = BusinessType.UPDATE)
    public Response<Void> update(@RequestBody @Valid PartnerUpdateDTO dto) {
        partnerService.update(dto);
        return Response.success();
    }

    @PostMapping("/switchStatus")
    @ApiOperation("切换合作商状态")
    @Log(businessType = BusinessType.UPDATE)
    public Response<Boolean> update(Long partnerId) {
        return Response.success(partnerService.switchStatus(partnerId));
    }

    @PostMapping("/remove")
    @ApiOperation("删除合作商")
    @Log(businessType = BusinessType.DELETE)
    public Response<Void> remove(Long partnerId) {
        partnerService.remove(partnerId);
        return Response.success();
    }

    @ApiOperation("刷新合作商缓存")
    @PostMapping("/refreshPartnerCache")
    public Response<String> refreshPartnerCache() {
        partnerService.refreshPartnerCache();
        return Response.success();
    }

}
